package com.origin.niuke.simulation;

/**
 * @author yzh
 * @version 1.0
 * @date 2022/7/6 22:56
 * 字符串变形
 * 算法：模拟
 */
public class NC89 {

    public String trans(String s, int n) {
        // write code here
        if (n == 1) return s;
        char[] arr = s.toCharArray();
        reverse(arr, 0, n - 1);
        int val = 'A' - 'a';
        for (int i = 0; i < n; i++) {
            if (arr[i] == ' ') continue;
            int j = i;
            while (j < n && arr[j] != ' ') {
                if (arr[j] >= 'a' && arr[j] <= 'z') arr[j] += val;
                else arr[j] -= val;
                ++j;
            }
            reverse(arr, i, j - 1);
            i = j;
        }
        return new String(arr);
    }
    void reverse(char[] arr, int l, int r) {
        while (l < r) {
            char tmp = arr[l];
            arr[l] = arr[r];
            arr[r] = tmp;
            ++l;
            --r;
        }
    }

}
